# -*- coding: utf-8 -*-

import psycopg2
import pymongo

MONGODB_SETTINGS = {
    'host': '192.168.1.198',
    'port': 27017
}

PSQL_SETTINGS = {
    # 'dbname': 'venti',
    'dbname': 'venti_20150701',
    'user': 'venti',
    'host': '192.168.1.198',
    'password': ''
}

# vacation_tables = ['vacation_classify', 'vacation_supplierconfirmoption', 'vacation_hotel', 'vacation_activityuser', 'vacation_activitytraveler', 'vacation_apptrialuser', 'vacation_blog', 'vacation_activity', 'vacation_datastats', 'vacation_destinationinfo', 'vacation_deviceid', 'vacation_exchange', 'vacation_expectdestination', 'vacation_feedback', 'vacation_destinationinfo_tags', 'vacation_destination', 'vacation_destinationoption', 'vacation_locality', 'vacation_orderproducttype', 'vacation_planordermtu', 'vacation_optionpoi', 'vacation_mtu', 'vacation_planorderpoi', 'vacation_orderrecognizee', 'vacation_optionmtu', 'vacation_itinerary', 'vacation_mtucomment', 'vacation_itineraryunit', 'vacation_itinerary_tags', 'vacation_payment', 'vacation_productorder', 'vacation_statistics', 'vacation_product', 'vacation_producttime', 'vacation_producttype', 'vacation_regionsurcharge', 'vacation_poidialog', 'vacation_supplierdestination', 'vacation_producttypeblackoutdate', 'vacation_productcustomfield', 'vacation_supplierproduct', 'vacation_supplier', 'vacation_userplan', 'vacation_supplierproducttype', 'vacation_template', 'vacation_tag', 'vacation_hotelproductorder', 'vacation_orderdetail', 'vacation_productcustomcategory']

vacation_tables = ['btmisc_devicetoken', 'btmisc_userdevicetoken']

conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" %
                        (PSQL_SETTINGS['dbname'],
                         PSQL_SETTINGS['user'],
                         PSQL_SETTINGS['host'],
                         PSQL_SETTINGS['password'])
                        )
cur = conn.cursor()


table2maxid = {}

for table in vacation_tables:
    _sql = """select max(id) from %s """ % table
    cur.execute(_sql)
    rows = cur.fetchall()
    table2maxid[table] = rows[0][0]

print table2maxid


client = pymongo.MongoClient(MONGODB_SETTINGS['host'], MONGODB_SETTINGS['port'])
vtc = client['vtc']
for table, maxid in table2maxid.items():
    print 'table [ %s ], count %s' % (table, maxid)
    if maxid > 0:
        vtc.psqlid2mongoid.insert([{'tbl': table, 'oldid': oldid} for oldid in range(1, maxid + 1)])
        print 'insert table [ %s ] done' % table
